package com.zero.server;

import java.awt.EventQueue;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

/**
 * 服务端类
 * 
 * @author Zero
 *
 */
public class Server {
	private static ServerSocket serverSocket;
	private static ServerView serverView;

	public static void main(String[] args) {

		// 创建服务器界面
		EventQueue.invokeLater(() -> {
			try {
				UIManager.setLookAndFeel("org.jvnet.substance.skin.SubstanceChallengerDeepLookAndFeel");
			} catch (Exception e) {
				SwingUtilities.invokeLater(
						() -> JOptionPane.showMessageDialog(null, "加载皮肤错误", "错误", JOptionPane.ERROR_MESSAGE));
			}
			
			serverView = new ServerView();
		});

		// 开启服务
		try {
			// 服务端套接字
			serverSocket = new ServerSocket(8080);
			while (true) {
				// 等待连接
				Socket socket = serverSocket.accept();
				// 在服务器端对客户端开启相应的线程
				new ServerIOThread(socket, serverView).start();
			}
		} catch (IOException e) {
			SwingUtilities.invokeLater(() -> {
				int result = JOptionPane.showOptionDialog(serverView, "请不要双开服务器！！！", "错误", JOptionPane.DEFAULT_OPTION,
						JOptionPane.ERROR_MESSAGE, null, null, null);
				if (result == JOptionPane.OK_OPTION || result == JOptionPane.CLOSED_OPTION) {
					System.exit(1);
				}
			});
		}
	}
}